<template>
	<view class="content b-t">
		<view class="list b-b" v-for="(item, index) in addressList" :key="index" >
			<view class="wrapper">
				<view class="u-box">
					<text class="name" style="color: #000000;">{{item.name}}</text>
					<text class="mobile">{{item.mobile}}</text>
				</view>
				<view class="address-box">
					<text v-if="item.is_default == 1" class="tag">默认地址</text>
					<text class="address">{{item.city_name}} {{item.district_name}}{{item.address}}</text>
				</view>
				
			</view>
			<!-- <text class="yticon icon-bianji" @click.stop="addAddress('edit', item)"></text> -->
			<view class="pull-right webkit">
				<!-- <view class="flex"></view> -->
				<view @tap="jump" :data-id="item.id">
					<image src="../../static/edit.png"></image>
					<text>编辑</text>
				</view>
				<view @click="del(item.id)">
					<image src="../../static/delete.png"></image>
					<text>删除</text>
				</view>
			</view>
		</view>
		
		<button class="add-btn" @click="jump">+新增收货地址</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list:[],
				user_id:'',
				res:[],
				// uid:{},
				source: 0,
				addressList: [],
				id:[],
				obj:{},
			}
		},
		onLoad(option){
			uni.getStorage({
				key: 'user_id',
				success: (res) => {
				 this.user_id = res.data
				 this.setajax()
				},
			});
		},
		methods: {
			setajax(){
				var o = this
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/Address/index?p=1',
					method: 'POST',
					data: {
						user_id: this.user_id,
					},
					header:{'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
					this.addressList = res.data.data.list
					var arr = [];
					this.addressList.forEach(function(item){
					 arr.push({id: item.id})
					})
					var obj = [];
					for(let i=0;i<arr.length;i++){
						o.obj = arr[i].id
						// console.log(o.obj)
					}
					// console.log(o.obj)
					},
					fail: () => {},
					complete: () => {}
				});
			},
			jump(e){
				 // event.stopPropagation();
				 uni.navigateTo({
				 	url: 'addressManage?id='+e.currentTarget.dataset.id
				 });
			},
			del(id){
				var o = this
				console.log(id)
				uni.showModal({
					content: "是否删除地址",
					confirmText: "确定",
					cancelText: "取消",
					success: res => {
						if(res.confirm){
							uni.showLoading()
							uni.request({
								url: 'https://cx.mingmeijt.com/Api/Address/deleteAddress',
								method: 'POST',
								data: {
									'user_id': this.user_id,
									'id': id
									},
								header:{'Content-Type':'application/x-www-form-urlencoded'},
								success: res => {
									console.log(res)
									uni.hideLoading()
									uni.showToast({
										title: res.data.msg
									})
									if(res.data.code == 200) {
										setTimeout(function() {
											o.setajax()
										}, 1500);
									}
								},
								fail: () => {},
								complete: () => {}
							});
						}
					},
					fail: () => {},
					complete: () => {}
				});
				
			},	
		}
	}
</script>

<style lang='scss'>
	page{
		padding-bottom: 120upx;
	}
	.content{
		position: relative;
	}
	.list{
		display: flex;
		align-items: center;
		padding: 20upx 30upx;;
		background: #fff;
		position: relative;
	}
	.wrapper{
		display: flex;
		flex-direction: column;
		flex: 1;
	}
	.address-box{
		display: flex;
		align-items: center;
		.tag{
			font-size: 24upx;
			color: #000;
			margin-right: 10upx;
			background: #fff;
			border: 1px solid #000;
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		}
		.address{
			font-size: 30upx;
			color: $font-color-dark;
		}
	}
	.u-box{
		font-size: 28upx;
		color: $font-color-light;
		margin-top: 16upx;
		.name{
			margin-right: 30upx;
		}
	}
	.icon-bianji{
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		color: $font-color-light;
		padding-left: 30upx;
	}
	.pull-right{float:right;}
	.pull-right view{margin-left:20upx;}
	.pull-right view image{width:34upx;height:34upx;position:relative;top:6upx;}
	.add-btn{
		position: fixed;
		left: 30upx;
		right: 30upx;
		bottom: 36upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		font-size: 32upx;
		color: #fff;
		background-color: #000;
		border-radius: 10upx;
		/* box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);		 */
	}
</style>
